package com.jojo.store.controller;

import com.jojo.store.controller.ex.*;
import com.jojo.store.entity.User;
import com.jojo.store.service.IUserService;
import com.jojo.store.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/users")
public class UserController extends BaseController{
    @Autowired
private IUserService userService;

    /***
     * 1,接收数据的方式，：请求处理方法的参数列表设置为pojo类型来接收前端的数据，
     * SpringBoot会见前端的url地址中的参数名称和pojo类的属性名进行比较
     * 如果这两个名称相等，则将值注入到pojo类中对应的属性中；
     * \
     * 但因为约定大于配置，选择使用同名称自动注入的SpringBoot配置来实现接受；
     *
     */

@PostMapping("/reg")
public JsonResult<java.lang.Void> reg(User user){

        userService.reg(user);

    return  new JsonResult<>(OK);
}

@PostMapping("/login")
public JsonResult<User> login(String username, String password, HttpSession session){
    User data = userService.login(username, password);
    session.setAttribute("uid",data.getUid());
    session.setAttribute("username",data.getUsername());

    return  new JsonResult<User>(OK,data);
    }


    
    @PostMapping("/change_password")
    public JsonResult<Void> changePwd(String oldPassword,String newPassword,HttpSession session){
        Integer uid = getUidBySession(session);
        String username = getUnmBySession(session);
        userService.ChangePwd(uid,username,oldPassword,newPassword);
        return  new JsonResult<Void>(OK);
    }
    @RequestMapping("/get_by_uid")
    public  JsonResult<User> getBid(HttpSession session){
        Integer uid = getUidBySession(session);
        User data = userService.getByUid(uid);
        return new JsonResult<User>(OK,data);
    }
    @RequestMapping("/change_info")
    public JsonResult<Void> changeUserdata(User user,HttpSession session){
        Integer uid = getUidBySession(session);
        String uname = getUnmBySession(session);
        userService.changeUserData(uid,uname,user);
        return  new JsonResult<Void>(OK);
    }
    public static final int AVATAR_MAX_SIZE=10*1024*1024;
    public static final List<String> AVATAR_TYPE= new ArrayList<>();
    static {
        AVATAR_TYPE.add("image/jpeg");
        AVATAR_TYPE.add("image/png");
        AVATAR_TYPE.add("image/bmp");
        AVATAR_TYPE.add("image/jpg");

    }
    @PostMapping("/change_avatar")
    public JsonResult<String> changeAvatar(HttpSession session,MultipartFile file){
        if (file.isEmpty()){
            throw new FileEmptyException("上传文件为空");
        }
        if (file.getSize()>AVATAR_MAX_SIZE){
            throw new FileSizeException("上传文件过大");
        }
        if (!AVATAR_TYPE.contains(file.getContentType())){
            throw new FileTypeException("上传文件类型不支持");
        }
        String dirname =session.getServletContext().getRealPath("uploads");
        File dir = new File(dirname);
        if (!dir.exists()){
            dir.mkdirs();
        }
        String filename = file.getOriginalFilename();
        int index = filename.lastIndexOf(".");
        //这里是获得小数点以及小数点后的所有值 也就是文件的后缀名字
        String suffix = filename.substring(index);
        String theFileName= UUID.randomUUID().toString().replace("-","")+suffix;
        File fileAdress = new File(dir, theFileName);
        try {
            file.transferTo(fileAdress);
        }catch (FileStateException e) {
            throw new FileStateException("文件状态异常");
        }
        catch (IOException e) {
           throw new FileUploadIOException("文件读写异常");
        }
        Integer uid = getUidBySession(session);
        String uname = getUnmBySession(session);
        String avatar= "/uploads/" +theFileName;
        userService.changeAvatar(uid,avatar,uname);
        return  new JsonResult<>(OK,avatar);
    }

}
